import { ref, reactive } from 'vue'
import { defineStore } from 'pinia'
import { storage } from '@/utils'
import { auth } from '@/api'

export const useAuthStore = defineStore('auth', () => {
  const tokenKey = 'token'
  const expiresInKey = 'expiresInKey'
  const userKey = 'user'
  const permissionsKey = 'permissions'
  const rolePermissionsKey = 'rolePermissions'
  const rolesKey = 'roles'
  const isItCertifiedKey = 'isItCertified'
  const userNameKey = 'userNameKey'
  const routerNamesKey = 'routerNamesKey'
  
  const token = ref('')
  const expiresIn = ref('')
  const userInfo = reactive({
	  user:{},
	  permissions:[],
	  rolePermissions:[],
	  roles:[],
	  isItCertified: ''
  })
  
  const getToken = () => token.value || ref(storage.getLocalStorage(tokenKey))
  const setToken = (data:string) => {
	  token.value = data
	  storage.setLocalStorage(tokenKey, data)
  }
  
  const getExpiresIn = () => expiresIn.value || ref(storage.getLocalStorage(expiresInKey))
  const setExpiresIn = (data:string) => {
  	  expiresIn.value = data
  	  storage.setLocalStorage(expiresInKey, data)
  }
  
  const setUserInfo = () => {
	  return new Promise((resolve, reject) => {
		 auth.getUserInfo().then((res:any) => {
			setInfo(res)
			setRoutes()
			storage.setLocalStorage(userNameKey, res.user.userName)
		 	resolve(userInfo)	  
		 }).catch((err:any) => {
			 reject(err)
		 }) 
	  })
  }
  
  const setRoutes = () => {
	  return new Promise((resolve, reject) => {
	  		 auth.getRouters().then((res:any) => {
				const data = res.data
				for(let i in data) {
					if(data[i].meta.title === '移动端') {
						const names:any = []
						data[i].children.map((item:any) => {
							names.push(item.meta.title)
							if(item.children) {
								item.children.map((item1:any) => {
									names.push(item1.meta.title)
								})
							}
						})
						
						storage.setLocalStorage(routerNamesKey, names)
						resolve(names)
					}
				}	  
	  		 }).catch((err:any) => {
	  			 reject(err)
	  		 }) 
	  })
  }
  
  const setInfo = (data:any) => {
	  userInfo.user = data.user
	  userInfo.permissions = data.permissions
	  userInfo.rolePermissions = data.rolePermissions
	  userInfo.roles = data.roles
	  userInfo.isItCertified = data.isItCertified
	  storage.setLocalStorage(userKey, data.user)
	  storage.setLocalStorage(permissionsKey, data.permissions)
	  storage.setLocalStorage(rolePermissionsKey, data.rolePermissions)
	  storage.setLocalStorage(rolesKey, data.roles)
	  storage.setLocalStorage(isItCertifiedKey, data.isItCertified)
  }
  
  const getUser = () => storage.getLocalStorage(userKey)  
  const getUserName = () => storage.getLocalStorage(userNameKey) 
  const getRouters = () => storage.getLocalStorage(routerNamesKey) 
  const getPermissions = () => storage.getLocalStorage(permissionsKey)
  const getRolePermissions = () => storage.getLocalStorage(rolePermissionsKey)
  const getRoles = () => storage.getLocalStorage(rolesKey)
  const getIsItCertified = () => storage.getLocalStorage(isItCertifiedKey)
  
  const getHasPermi = (value:string) => {
	  return new Promise((resolve, reject) => {
		  const permissions: any = getRolePermissions()
		  const permissionApp = permissions.APP
		  if (value && value.length > 0) {
			
		    let hasPermissions = false
			 hasPermissions = permissionApp.some((permission: any) => {
				return value ===  permission
		    })
			resolve(hasPermissions)
		  }
	  })
  }
  
  const hasAppRole = () => {
	  return getRoles().includes('APP')
  }
  
  return { 
	  token, 
	  expiresIn, 
	  getToken,
	  setToken,
	  getExpiresIn,
	  setExpiresIn,
	  setInfo,
	  setUserInfo,
	  getUser,
	  getUserName,
	  getPermissions,
	  getRolePermissions,
	  getRoles,
	  getIsItCertified,
	  setRoutes,
	  getRouters,
	  getHasPermi,
	  hasAppRole
	}
})